'''
Created on Mar 8, 2010

@author: sternron
'''
import graph
import logging
import sys
import cliques.randomCliqueSearch
import orchid
import re
from cliques.randomCliqueSearch import randomCliqueSearch
from cliques.knownDegreeSearch import knownDegreeSearch
from cliques.cliqueStarSearch import cliqueStarSearch
from cliques.cliqueSearchLowerbound import CliqueSearchLowerbound
from cliques.mdp.mdpBasedSearch import mdpBasedSearch
from cliques.mdp.limitedSampling import LimitedSampling
from cliques.closest.closestCliqueSearch import ClosestCliqueSearch
from cliques.closest.breadthFirstSearch import BreadthFirstSearch
import timerManager
import os
import urllib2
import time
import random
from webgraphs.onlineClosestCliqueSearch import OnlineClosestCliqueSearch
#import urllib2.HTTPError
import onlineCliqueSearch

RESOURCE_RESULTS_DIR = "../resources/results"
RESOURCE_ROOT_PAGES_FILE = "../resources/nanotoxic/papersOver3.txt"
  
def main(): 
    logging.basicConfig(level=logging.DEBUG)
    logging.root.addHandler(logging.StreamHandler(sys.stdout))
   
    # Load root pages
    source_pages_file = open(RESOURCE_ROOT_PAGES_FILE,'r')
    start_nodes = []
    for line in source_pages_file:
        line = line.strip()
        if line.endswith(".pdf"):
            line = line[:-4]
        start_nodes.append("http://scholar.google.com/scholar?q=%s&lr=lang_en&num=100"%line);

    clique_algorithm = BreadthFirstSearch()
    
    graph_counter=0
    exploration_limit = len(start_nodes) 
    searcher = OnlineClosestCliqueSearch(clique_algorithm) 
    root_page = start_nodes[0]      
    k=5 # Doesn't really matter as long as larger than 3      
    try:                            
        logging.info("Starting %d-clique search with %s !!" % (k, clique_algorithm))
        searcher.timer_manager.create_timer('total_runtime')
        searcher.timer_manager.create_timer('exploration_runtime')
        searcher.timer_manager.create_timer('choose_node_runtime')
        searcher.timer_manager.create_timer('expand_runtime')

        searcher.run(start_nodes[0], k,exploration_limit,start_nodes[1:])                
    except urllib2.HTTPError, error:
        logging.exception(error)
        logging.error(error.read())
        logging.info("Blocked after %d nodes" % len(searcher.ckg.nodes()))
        out_file.close()
        sys.exit()
                                
    # Output the graph
    searcher.explort_ckg("%s/CKG_Graph%d.dot" % (RESOURCE_RESULTS_DIR,graph_counter), \
                                host_file_name="%s/Graph%d.nodes.txt" % (RESOURCE_RESULTS_DIR,graph_counter))

if __name__ == '__main__':
    main()